﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Douban.Api.Infrastructure;

namespace Douban.Api.User
{
    public class UserServiceProvider
    {
        private const string UserServiceUrl = "http://api.douban.com/people";
        private UserDetail _currentUser;

        public DoubanContext Context { get; set; }

        public UserDetail CurrentUser
        {
            get
            {
                if (_currentUser == null)
                    _currentUser = GetCurrentUser();

                return _currentUser;
            }
        }

        public UserDetail GetUserDetail(string userId)
        {
            return Context.GetResource<UserDetail>(userId);
        }

        public UserDetail GetCurrentUser()
        {
            _currentUser = Context.GetProtectedResource<UserDetail>(UserServiceUrl + HttpUtility.UrlEncode("/@me"));
            return _currentUser;
        }

        public UserResults SearchUsers(string query, int startIndex = 1, int maxResults = 50)
        {
            return Context.GetResource<UserResults>(UserServiceUrl + "?q=" + query, startIndex, maxResults);
        }

        public UserResults GetFriends(string userId, int startIndex = 1, int maxResults = 50)
        {
            return Context.GetProtectedResource<UserResults>(userId + "/friends", startIndex, maxResults);
        }

        public UserResults GetContacts(string userId, int startIndex = 1, int maxResults = 50)
        {
            return Context.GetResource<UserResults>(userId + "/contacts", startIndex, maxResults);
        }
    }
}
